package com.emergency.db.controller.item;

import com.emergency.common.entity.Result;
import com.emergency.common.entity.StatusCode;
import com.emergency.db.domain.item.TinyItemStatus;
import com.emergency.db.service.item.TinyItemStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tinyitem/status")
@CrossOrigin
public class TinyItemStatusController {
    @Autowired
    private TinyItemStatusService tinyItemStatusService;

    //保存
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody TinyItemStatus tinyItemStatus) {
        Long id = tinyItemStatusService.save(tinyItemStatus);
        return new Result(true, StatusCode.OK, "保存成功", id);
    }

    //查询单项
    @RequestMapping(value = "/search/{id}", method = RequestMethod.POST)
    public Result findById(@PathVariable("id") Long id) {
        TinyItemStatus tinyItemStatus = tinyItemStatusService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", tinyItemStatus);
    }

    //查询单项ByItem
    @RequestMapping(value = "/search/item/{id}", method = RequestMethod.POST)
    public Result findAllById(@PathVariable("id") Long id) {
        List<TinyItemStatus> tinyItemStatusList = tinyItemStatusService.findAllByItem(id);
        return new Result(true, StatusCode.OK, "查询成功", tinyItemStatusList);
    }

    //删除
    @RequestMapping(value = "/del/{id}", method = RequestMethod.POST)
    public Result delete(@PathVariable Long id) {
        tinyItemStatusService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    //删除ByItem
    @RequestMapping(value = "/del/item/{id}", method = RequestMethod.POST)
    public Result deleteAllByItem(@PathVariable Long id) {
        tinyItemStatusService.deleteAllByItem(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    //分页
    @RequestMapping(value = "list/{page}/{size}", method = RequestMethod.POST)
    public Result list(@RequestBody Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<TinyItemStatus> pageList = tinyItemStatusService.findList(searchMap, page, size);
        return new Result(true, StatusCode.OK, "操作成功", pageList);
    }
}
